//
//  MBGoodsTableViewCell.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/23.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBGoodsTableViewCell.h"
#import "MBGoodsIrregularitiesPopView.h"


@interface MBGoodsTableViewCell ()
@property (nonatomic,weak)UIImageView* goodsImageView;
@property (nonatomic,weak)UILabel* titleLabel;
@property (nonatomic,weak)UILabel* numLabel;
@property (nonatomic,weak)UILabel* moneyLabel;

@property (nonatomic,weak)UIButton* irregularitiesButton;
@property (nonatomic,weak)CAGradientLayer* gradientLayer0;

@end

@implementation MBGoodsTableViewCell
@dynamic viewModel;

-(void)setupLayout{
    [super setupLayout];
    
    UIImageView* goodsImageView = [[UIImageView alloc]init];
    self.goodsImageView = goodsImageView;
    [self.contentView addSubview:goodsImageView];
    
    [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset([UIView lf_sizeFromIphone6:12]);
        make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:90], [UIView lf_sizeFromIphone6:90]));
    }];
    
    UILabel* titleLabel = [[UILabel alloc]init];
    self.titleLabel = titleLabel;
    titleLabel.numberOfLines = 2;
    [self.contentView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(goodsImageView).offset([UIView lf_sizeFromIphone6:4]);
        make.left.equalTo(goodsImageView.mas_right).offset([UIView lf_sizeFromIphone6:10]);
        make.right.equalTo(self.contentView).offset([UIView lf_sizeFromIphone6:-30]);
    }];
    
    UILabel* numLabel = [[UILabel alloc]init];
    self.numLabel = numLabel;
    numLabel.text = @"库存66";
    numLabel.font = [UIFont lf_mediumFontOfSize:12];
    numLabel.textColor = [UIColor colorWithR:177 G:176 B:177];
    [self.contentView addSubview:numLabel];
    
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.top.equalTo(titleLabel.mas_bottom).offset([UIView lf_sizeFromIphone6:10]);
    }];
    
    UILabel* moneyLabel = [[UILabel alloc]init];
    self.moneyLabel = moneyLabel;
    moneyLabel.text = @"￥1250.00";
    moneyLabel.font = [UIFont lf_mediumFontOfSize:15];
    moneyLabel.textColor = [UIColor colorWithR:237 G:85 B:89];
    [self.contentView addSubview:moneyLabel];
    
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.bottom.equalTo(goodsImageView).offset([UIView lf_sizeFromIphone6:-1]);
    }];
    
    UIButton* irregularitiesButton = [[UIButton alloc]init];
    irregularitiesButton.layer.cornerRadius = [UIView lf_sizeFromIphone6:25/2];
    irregularitiesButton.layer.masksToBounds = YES;
    CAGradientLayer *gradientLayer0 = [[CAGradientLayer alloc] init];
    self.gradientLayer0 = gradientLayer0;
    gradientLayer0.locations = @[@0, @1];
    [gradientLayer0 setStartPoint:CGPointMake(0, 0)];
    [gradientLayer0 setEndPoint:CGPointMake(1, 1)];
    [irregularitiesButton.layer addSublayer:gradientLayer0];
    self.irregularitiesButton = irregularitiesButton;
    [irregularitiesButton addTarget:self action:@selector(handleClickIrregularitiesButton) forControlEvents:UIControlEventTouchUpInside];
    irregularitiesButton = irregularitiesButton;
    irregularitiesButton.titleLabel.font = [UIFont lf_systemFontOfSize:12];
    [self.contentView addSubview:irregularitiesButton];
    
    [irregularitiesButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset([UIView lf_sizeFromIphone6:-13]);
        make.bottom.equalTo(self.contentView).offset([UIView lf_sizeFromIphone6:-11]);
        make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:58], [UIView lf_sizeFromIphone6:25]));
    }];
    
}

-(void)setupData
{
    [super setupData];
    
    NSString* cacheUrl = [NSString stringWithFormat:@"%ld",(long)self.viewModel.dataModel.goods_id];
    UIImage* image = [[MBCacheService sharedInstance]fetchHeaderWithHeaderName:cacheUrl];
    
    if (image) {
        self.goodsImageView.image = image;
    }else {
        [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/goods/goodsThumImages/goods_id/%ld/width/200/height/200",BASE_URL,(long)self.viewModel.dataModel.goods_id]] placeholderImage:[UIImage imageFromColor:COLOR_BG forSize:CGSizeMake([UIView lf_sizeFromIphone6:90], [UIView lf_sizeFromIphone6:90]) withCornerRadius:[UIView lf_sizeFromIphone6:6]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
                [[MBCacheService sharedInstance]cacheHeaderWithHeaderName:cacheUrl withHeader:image];
            }
        }];
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:[UIView lf_sizeFromIphone6:3]];
    NSMutableAttributedString* attStr = [[NSMutableAttributedString alloc]initWithString:self.viewModel.dataModel.goods_name attributes:@{NSParagraphStyleAttributeName:paragraphStyle,NSFontAttributeName:[UIFont lf_mediumFontOfSize:13],NSForegroundColorAttributeName:[UIColor colorWithR:45 G:45 B:45]}];
    self.titleLabel.attributedText = attStr;
    self.moneyLabel.text = [NSString stringWithFormat:@"%@",self.viewModel.dataModel.shop_price];
    self.numLabel.text = [NSString stringWithFormat:@"库存%ld",(long)self.viewModel.dataModel.store_count];
    
    if (self.viewModel.dataModel.is_on_sale == 1) {
        self.gradientLayer0.frame = CGRectMake(0, 0, [UIView lf_sizeFromIphone6:58], [UIView lf_sizeFromIphone6:25]);
        self.gradientLayer0.colors =@[
                                 (id)[UIColor colorWithRed:234.0f/255.0f green:62.0f/255.0f blue:63.0f/255.0f alpha:1.0f].CGColor,
                                 (id)[UIColor colorWithRed:254.0f/255.0f green:145.0f/255.0f blue:114.0f/255.0f alpha:1.0f].CGColor];

        [self.irregularitiesButton setTitle:@"下架" forState:UIControlStateNormal];
        [self.irregularitiesButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:58], [UIView lf_sizeFromIphone6:25]));
        }];
    }else if (self.viewModel.dataModel.is_on_sale == 0){
        self.gradientLayer0.frame = CGRectMake(0, 0, [UIView lf_sizeFromIphone6:58], [UIView lf_sizeFromIphone6:25]);
        self.gradientLayer0.colors =@[
                                 (id)[UIColor colorWithRed:255.0f/255.0f green:99.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor,
                                 (id)[UIColor colorWithRed:255.0f/255.0f green:136.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor];
        [self.irregularitiesButton setTitle:@"上架" forState:UIControlStateNormal];
        [self.irregularitiesButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:58], [UIView lf_sizeFromIphone6:25]));
        }];

    }else {
        self.gradientLayer0.frame = CGRectMake(0, 0, [UIView lf_sizeFromIphone6:82], [UIView lf_sizeFromIphone6:25]);
        self.gradientLayer0.colors =@[
                                 (id)[UIColor colorWithRed:255.0f/255.0f green:99.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor,
                                 (id)[UIColor colorWithRed:255.0f/255.0f green:136.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor];
        [self.irregularitiesButton setTitle:@"违规信息" forState:UIControlStateNormal];
        [self.irregularitiesButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:82], [UIView lf_sizeFromIphone6:25]));
        }];
    }
}

-(void)handleClickIrregularitiesButton
{
    if (self.viewModel.dataModel.is_on_sale == 1 || self.viewModel.dataModel.is_on_sale == 0) {
        [self.viewModel goodsLower];
    }else {
        MBGoodsIrregularitiesPopView* popView = [[MBGoodsIrregularitiesPopView alloc]init];
        [popView show];
    }
}

// 用来计算高度
+(NSNumber*)calculateRowHeightWithViewModel:(MBGoodsTableViewCellModel *)viewModel
{
    return [viewModel cacheCellHeightWithCalculateBlock:^NSNumber * _Nonnull{
        return viewModel.totalHeight;
    }];
}


@end
